package com.cyanite.playlv2;

import com.cyanite.playlv2.util.NotificationsUtil;
import com.cyanite.playlv2.util.RemoteResourceManager;
import com.cyanite.playlv2.util.UiUtil;
import com.cyanite.playlv2.widget.PhotoStrip;
import com.cyanite.playlv2.widget.RecommendTripAdapter;
import com.cyanite.playlv2api.PlayLv2Api;
import com.cyanite.playlv2api.types.Group;
import com.cyanite.playlv2api.types.Response;
import com.cyanite.playlv2api.types.Trip;
import com.cyanite.playlv2api.types.TripMini;
import com.cyanite.playlv2api.util.ConstantsUtils.TripProcessConsts;
import com.cyanite.playlv2.R;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.KeyEvent;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

public class TripDetailsActivity extends Activity {

    public static final String EXTRA_TRIP_PARCEL = PlayLv2.PACKAGE_NAME
        + ".TripDetailsActivity.EXTRA_TRIP_PARCEL";
    public static final String EXTRA_TRIP_ID = PlayLv2.PACKAGE_NAME
        + ".TripDetailsActivity.EXTRA_TRIP_ID";

    private static final int ACTIVITY_TRIP_DETAIL = 815;
    private static final int LOAD_TYPE_TRIP_NONE = 0;
    private static final int LOAD_TYPE_TRIP_ID = 1;
    private static final int LOAD_TYPE_TRIP_PARTIAL = 2;
    private static final int LOAD_TYPE_TRIP_FULL = 3;

    private static final int MENU_GROUP_SEARCH = 0;
    private static final int MENU_REFRESH = 1;

    private StateHolder mStateHolder;
    private RemoteResourceManager mRrm;
    private RemoteResourceManagerObserver1 mResourcesObserver1;
    private RemoteResourceManagerObserver2 mResourcesObserver2;
    private Handler mHandler1;
    private Handler mHandler2;
    private Gallery mGallery;

    private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.trip_details_activity);
        registerReceiver(mLoggedOutReceiver, new IntentFilter(PlayLv2.INTENT_ACTION_LOGGED_OUT));

        mStateHolder = new StateHolder();
        if (getIntent().hasExtra(EXTRA_TRIP_PARCEL)) {
            Trip trip = getIntent().getExtras().getParcelable(EXTRA_TRIP_PARCEL);
            mStateHolder.setTrip(trip);
            mStateHolder.setTripId(trip.getId());
            mStateHolder.setLoadType(LOAD_TYPE_TRIP_PARTIAL);
        }
        if (getIntent().hasExtra(EXTRA_TRIP_ID)) {
            mStateHolder.setTripId(getIntent().getExtras().getString(EXTRA_TRIP_ID));
            mStateHolder.setLoadType(LOAD_TYPE_TRIP_ID);
        }
        mStateHolder.setUserId(((PlayLv2) getApplication()).getUserId());

        mHandler1 = new Handler();
        mRrm = ((PlayLv2) getApplication()).getRemoteResourceManager();
        mResourcesObserver1 = new RemoteResourceManagerObserver1();
        mRrm.addObserver(mResourcesObserver1);
        mHandler2 = new Handler();
        mResourcesObserver2 = new RemoteResourceManagerObserver2();
        mRrm.addObserver(mResourcesObserver2);

        if (mStateHolder.getLoadType() != LOAD_TYPE_TRIP_FULL &&
           !mStateHolder.getIsRunningTripDetailsTask() &&
           !mStateHolder.getRanOnce()) {
            mStateHolder.startTaskTripDetails(this);
        }

        ensureUi();
    }

    @Override
    public void onPause() {
        super.onPause();

        if (isFinishing()) {
            mStateHolder.cancelTasks();
            mHandler1.removeCallbacks(mRunnableUpdateTripPhoto);
            mHandler2.removeCallbacks(mRunnableUpdateUserPhoto);
            mRrm.deleteObserver(mResourcesObserver1);
            mRrm.deleteObserver(mResourcesObserver2);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mLoggedOutReceiver);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }else {
            return super.onKeyDown(keyCode, event);
        }
    }

    private void ensureUi() {
        setTitle(getString(R.string.trip_details_activity_label));

        int sdk = UiUtil.sdkVersion();
        View viewProgressBar = findViewById(R.id.tripDetailsProgress);
        TextView tvTripName = (TextView)findViewById(R.id.tvTripName);
        TextView tvTripProcess = (TextView)findViewById(R.id.tvTripProcess);
        TextView tvTripAddress = (TextView)findViewById(R.id.tvTripaddress);
        Button btnFocused = (Button)findViewById(R.id.btnFocused);

        View viewAuthor = findViewById(R.id.tripDetailsActivityUser);
        View viewTasks = findViewById(R.id.tripDetailActivityGeneralTasks);
        View viewCheckins = findViewById(R.id.tripDetailsActivityGeneralCheckins);
        View viewTaskAndCheckin = findViewById(R.id.tripDetailsActivityGeneral);

        TextView tvTasks = (TextView)findViewById(R.id.tvTasksValue);
        TextView tvCheckins = (TextView)findViewById(R.id.tvCheckinsValue);
        ImageView ivTasksChevron = (ImageView)findViewById(R.id.imgTasksChevron);
        ImageView ivCheckinsChevron = (ImageView)findViewById(R.id.imgCheckinsChevron);

        View viewFriends = findViewById(R.id.tripDetailsActivityFriends);
        TextView tvFriends = (TextView)findViewById(R.id.tripDetailsActivityFriendsText);
        PhotoStrip psFriendsPhotos = (PhotoStrip)findViewById(R.id.tripDetailsActivityFriendsPhotos);
        ImageView ivFriendsChevron = (ImageView)findViewById(R.id.tripDetailsActivityFriendsChevron);
        
        View viewTrips = findViewById(R.id.tripDetailsActivityTrips);
        viewProgressBar.setVisibility(View.VISIBLE);

        tvTasks.setText("0");
        tvCheckins.setText("0");

        viewTasks.setFocusable(false);
        viewCheckins.setFocusable(false);
        viewFriends.setFocusable(false);
        viewAuthor.setFocusable(false);
        viewTrips.setFocusable(false);

        viewFriends.setVisibility(View.GONE);
        ivCheckinsChevron.setVisibility(View.INVISIBLE);
        ivTasksChevron.setVisibility(View.INVISIBLE);
        ivFriendsChevron.setVisibility(View.INVISIBLE);
        psFriendsPhotos.setVisibility(View.GONE);
        viewAuthor.setVisibility(View.GONE);
        viewTrips.setVisibility(View.GONE);

        if (mStateHolder.getLoadType() >= LOAD_TYPE_TRIP_PARTIAL) {
            Trip trip = mStateHolder.getTrip();
            ensureTripPhoto(trip.getPhoto());


            if(trip.getStartDate() == null){
                tvTripProcess.setText(getString(TripProcessConsts.getTripProcessRes(trip.getProcess())));
            }else{
                String strDate =  String.format(getString(R.string.trip_total_day_prompt), trip.getTotalDays()) + "  " +
                            getString(TripProcessConsts.getTripProcessRes(trip.getProcess())) +"  " +
                            getString(R.string.trip_start_day_prompt)+
                            trip.getStartDate();
                tvTripProcess.setText(strDate);
            }
            String strAddress = new String();
            if(trip.getStartpoint()!= null ){
                strAddress+=trip.getStartpoint();
                tvTripAddress.setText(trip.getStartpoint()+"-"+trip.getEndpoint());
            }else if(trip.getEndpoint()!=null){
                strAddress+="-"+trip.getEndpoint();
            }
            
            tvTripAddress.setText(strAddress);
            tvTripName.setText(trip.getName());

            if ( mStateHolder.getTrip().getUserId().equals(mStateHolder.getUserId()) )   {
                btnFocused.setVisibility(View.VISIBLE);
                btnFocused.setText(getString(R.string.trip_details_activity_invite_focused));
                btnFocused.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startAddFollowsActivity();
                    }
                });
            }

            if (mStateHolder.getLoadType() >= LOAD_TYPE_TRIP_FULL) {
                viewProgressBar.setVisibility(View.GONE);
                viewTaskAndCheckin.setVisibility(View.VISIBLE);
                viewAuthor.setVisibility(View.VISIBLE);
                if (!mStateHolder.getTrip().getUserId().equals(mStateHolder.getUserId())) {
                    viewTrips.setVisibility(View.VISIBLE);
                    viewTrips.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(TripDetailsActivity.this, UserTripsActivity.class);
                            intent.putExtra(UserTripsActivity.EXTRA_USER_ID, mStateHolder.getTrip().getUserId());
                            startActivity(intent);
                        }
                    });
                }

                TextView tvUserTitle = (TextView)findViewById(R.id.tvTripDetailsActivityUserName);
                tvUserTitle.setText(trip.getUserName());
                ensureUserPhoto(trip);

                tvTasks.setText(String.valueOf(trip.getTasksCount()));
                if ( trip.getTasksCount() > 0 ) {
                    viewTasks.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startTasksActivity();
                        }
                    });
                    viewTasks.setFocusable(true);
                    if (sdk > 3) {
                        ivTasksChevron.setVisibility(View.VISIBLE);
                    }
                }

                tvCheckins.setText(String.valueOf(trip.getCheckinsCount()));
                if (trip.getCheckinsCount() > 0) {
                    viewCheckins.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startCheckinsActivity();
                        }
                    });
                    viewCheckins.setFocusable(true);
                    if (sdk > 3) {
                        ivCheckinsChevron.setVisibility(View.VISIBLE);
                    }
                }

                if ( !mStateHolder.getTrip().getUserId().equals(mStateHolder.getUserId()) )   {
                    btnFocused.setVisibility(View.VISIBLE);
                    if ( !trip.isFocused() ){
                        btnFocused.setText(getString(R.string.trip_details_activity_add_focused));
                        btnFocused.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mStateHolder.startTaskFocused(TripDetailsActivity.this, true);
                            }
                        });
                    } else {
                        btnFocused.setText(getString(R.string.trip_details_activity_remove_focused));
                        btnFocused.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                view.setEnabled(false);
                                mStateHolder.startTaskFocused(TripDetailsActivity.this, false);
                            }
                        });
                    }
                }

                if ( trip.getFocusedCount() > 0 ) {
                    ivFriendsChevron.setVisibility(View.VISIBLE);
                    viewFriends.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startFocusedActivity();
                        }
                    });
                    if (trip.getFriendsInCommon() != null && trip.getFriendsInCommon().size() > 0)   {
                        psFriendsPhotos.setVisibility(View.VISIBLE);
                        psFriendsPhotos.setUsersAndRemoteResourcesManager(trip.getFriendsInCommon(), mRrm);
                    }   else {
                        tvFriends.setPadding(tvFriends.getPaddingLeft(), tvCheckins.getPaddingTop(),
                                tvFriends.getPaddingRight(), tvCheckins.getPaddingBottom());
                    }
                    tvFriends.setText(getResources().getString(R.string.trip_details_activity_focused_text, trip.getFocusedCount()));
                    viewFriends.setVisibility(View.VISIBLE);
                    viewFriends.setFocusable(true);
                }

                ensureRecommendTrips();
            } else {
                // Haven't done a full load.
                if (mStateHolder.getRanOnce()) {
                    viewProgressBar.setVisibility(View.GONE);
                }
            }
        } else {
            // Haven't done a full load.
            if (mStateHolder.getRanOnce()) {
                viewProgressBar.setVisibility(View.GONE);
            }
        }

        // Regardless of load state, if running a task, show titlebar progress bar.
        if (mStateHolder.getIsTaskRunning()) {
            setProgressBarIndeterminateVisibility(true);
        } else {
            setProgressBarIndeterminateVisibility(false);
        }

        // Disable focused button if running friend task.
        if (mStateHolder.getIsRunningFocusedTask()) {
            btnFocused.setEnabled(false);
        } else {
            btnFocused.setEnabled(true);
        }
    }

    private void ensureRecommendTrips(){
        Group<TripMini> trips = mStateHolder.getTrip().getTripsRecommend();
        View viewRecommend = findViewById(R.id.llRecommend);
        if (trips != null && trips.size() > 0)  {
            mGallery = (Gallery)findViewById(R.id.galleryRecommendTrip);
            mGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    TripMini trip = (TripMini) arg0.getAdapter().getItem(arg2);
                    Intent intent = new Intent(TripDetailsActivity.this, TripDetailsActivity.class);
                    intent.putExtra(TripDetailsActivity.EXTRA_TRIP_ID, trip.getId());
                    startActivityForResult(intent, ACTIVITY_TRIP_DETAIL);
                    // in order to avoid by user click recommend trips continueously
                    finish();
                }
            });
            RecommendTripAdapter adapter = new RecommendTripAdapter(this,
                        ((PlayLv2) getApplication()).getRemoteResourceManager());
            adapter.setGroup(trips);
            mGallery.setAdapter(adapter);
            viewRecommend.setVisibility(View.VISIBLE);
        }else{
            viewRecommend.setVisibility(View.GONE);
        }
    }

    private void ensureTripPhoto(String photoUrl) {
        ImageView ivPhoto = (ImageView)findViewById(R.id.imgTripPhoto);
        ivPhoto.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TripDetailsActivity.this, TripPhotosActivity.class);
                intent.putExtra(TripPhotosActivity.EXTRA_TRIP_ID, mStateHolder.getTrip().getId());
                startActivity(intent);
            }
        });

        if (photoUrl == null) {
            ivPhoto.setImageResource(R.drawable.trip_default_photo);
            return;
        }

        Uri uriPhoto = Uri.parse(photoUrl);
        if (mRrm.exists(uriPhoto)) {
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(mRrm.getInputStream(uriPhoto));
                ivPhoto.setImageBitmap(bitmap);
            } catch (IOException e) {
                ivPhoto.setImageResource(R.drawable.trip_default_photo);
            }
        } else {
            mRrm.request(uriPhoto);
            ivPhoto.setImageResource(R.drawable.trip_default_photo);
        }
        ivPhoto.postInvalidate();
    }

    private void ensureUserPhoto(Trip trip) {
        View viewUser = findViewById(R.id.tripDetailsActivityUser);
        viewUser.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startUserDetailActivity();
            }
        });

        ImageView ivUserPhoto = (ImageView)findViewById(R.id.tripDetailsActivityUserPhoto);
        if (trip.getUserPhoto() == null) {
            ivUserPhoto.setImageResource(R.drawable.blank_girl);
            return;
        }

        Uri uriPhoto = Uri.parse(trip.getUserPhoto());
        if (mRrm.exists(uriPhoto)) {
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(mRrm.getInputStream(uriPhoto));
                ivUserPhoto.setImageBitmap(bitmap);
            } catch (IOException e) {
                ivUserPhoto.setImageResource(R.drawable.blank_girl);
            }
        } else {
            mRrm.request(uriPhoto);
            ivUserPhoto.setImageResource(R.drawable.blank_girl);
        }
        ivUserPhoto.postInvalidate();
    }

    private void startUserDetailActivity() {
        Intent intent = new Intent(TripDetailsActivity.this, UserDetailsActivity.class);
        intent.putExtra(UserDetailsActivity.EXTRA_USER_ID, mStateHolder.getTrip().getUserId());
        startActivity(intent);
    }

    private void startTasksActivity() {
        Intent intent = new Intent(TripDetailsActivity.this, TaskCategoryActivity.class);
        intent.putExtra(TaskCategoryActivity.EXTRA_TRIP_PARCEL, mStateHolder.getTrip());
        startActivity(intent);
    }

    private void startFocusedActivity() {
        Trip trip = mStateHolder.getTrip();
        Intent intent = new Intent(TripDetailsActivity.this, UserDetailsFriendsInCommonActivity.class);
        intent.putExtra(UserDetailsFriendsInCommonActivity.EXTRA_TRIP_PARCEL, trip);
        intent.putExtra(UserDetailsFriendsInCommonActivity.EXTRA_TRIP_FOCUSED_USER, "1");
        startActivity(intent);
    }

    private void startAddFollowsActivity() {
        Intent intent = new Intent(TripDetailsActivity.this, AddFollowsActivity.class);
        intent.putExtra(AddFollowsActivity.EXTRA_TRIP_ID, mStateHolder.getTrip().getId());
        startActivity(intent);
    }

    private void startCheckinsActivity() {
        Intent intent = new Intent(TripDetailsActivity.this, TasksActivity.class);
        intent.putExtra(TasksActivity.EXTRA_TRIP_ID, mStateHolder.getTrip().getId());
        startActivity(intent);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(MENU_GROUP_SEARCH, MENU_REFRESH, Menu.NONE, R.string.refresh)
        .setIcon(R.drawable.ic_menu_refresh);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_REFRESH:
                mStateHolder.startTaskTripDetails(this);
                return true;
            case MENU_GROUP_SEARCH:
                return true;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void onTripDetailsTaskComplete(Trip trip, Exception ex) {
        mStateHolder.setIsRunningTripDetailsTask(false);
        mStateHolder.setRanOnce(true);
        if (trip != null) {
            mStateHolder.setTrip(trip);
            mStateHolder.setLoadType(LOAD_TYPE_TRIP_FULL);
            ensureUi();
        } else if (ex != null) {
            NotificationsUtil.ToastReasonForFailure(this, ex);
        } else {
            Toast.makeText(this, R.string.error_unknow, Toast.LENGTH_LONG).show();
        }
    }

    private static class TripDetailsTask extends AsyncTask<String, Void, Trip> {

        private TripDetailsActivity mActivity;
        private Exception mReason;
        private String mTripId;

        public TripDetailsTask(TripDetailsActivity activity, String tripId) {
            mActivity = activity;
            mTripId = tripId;
        }

        @Override
        protected void onPreExecute() {
            mActivity.ensureUi();
        }

        @Override
        protected Trip doInBackground(String... params) {
            try {
                return ((PlayLv2) mActivity.getApplication()).getPlayLv2Api().trip(mTripId);
            } catch (Exception ex) {
                mReason = ex;
                return null;
            }
        }

        @Override
        protected void onPostExecute(Trip trip) {
            if (mActivity != null) {
                mActivity.onTripDetailsTaskComplete(trip, mReason);
            }
        }

        @Override
        protected void onCancelled() {
            if (mActivity != null) {
                mActivity.onTripDetailsTaskComplete(null, mReason);
            }
        }

        public void setActivity(TripDetailsActivity activity) {
            mActivity = activity;
        }
    }

    private void onFocusedTaskComplete(Response response, Exception ex) {
        mStateHolder.setIsRunningFocusedTask(false);
        if (response != null && response.getValue().equals("0")) {
            mStateHolder.getTrip().setFocused(!mStateHolder.getTrip().isFocused());
        } else {
            NotificationsUtil.ToastReasonForFailure(this, ex);
        }
        ensureUi();
    }

    private static class FocusedTask extends AsyncTask<Void, Void, Response> {

        private TripDetailsActivity mActivity;
        private String mTripId;
        private boolean mIsFocused;
        private Exception mReason;

        public FocusedTask(TripDetailsActivity activity, String tripId, boolean isFocused) {
            mActivity = activity;
            mTripId = tripId;
            mIsFocused = isFocused;
        }

        @Override
        protected void onPreExecute() {
            mActivity.ensureUi();
        }

        @Override
        protected Response doInBackground(Void... params) {
            PlayLv2Api playlv2api = ((PlayLv2) mActivity.getApplication()).getPlayLv2Api();
            try {
               Response response = playlv2api.tripFocusedRequest(mTripId, mIsFocused);
               if (response == null)    response = new Response();
               return response;
            } catch (Exception ex) {
                mReason = ex;
                return null;
            }
        }

        @Override
        protected void onPostExecute(Response response) {
            if (mActivity != null) {
                mActivity.onFocusedTaskComplete(response, mReason);
            }
        }

        @Override
        protected void onCancelled() {
            if (mActivity != null) {
                mActivity.onFocusedTaskComplete(null, mReason);
            }
        }
    }

    private static class StateHolder {

        private Trip mTrip;
        private String mTripId;
        private String mUserId;
        private TripDetailsTask mTaskTripDetails;
        private FocusedTask mTaskFocused;
        private boolean mRanOnce;
        private int mLoadType;
        private boolean mIsRunningTripDetailsTask;
        private boolean mIsRunningFocusedTask;

        public StateHolder() {
            mIsRunningTripDetailsTask = false;
            mIsRunningFocusedTask = false;
            mRanOnce = false;
            mLoadType = LOAD_TYPE_TRIP_NONE;
        }

        public Trip getTrip() {
            return mTrip;
        }

        public void setTrip(Trip trip) {
            mTrip = trip;
            if (mTrip.getId() == null || mTrip.getId().equals(""))   {
                mTrip.setId(mTripId);
            }
        }

        public void setTripId(String id)    {
            mTripId = id;
        }

        public String getUserId()    {
            return mUserId;
        }

        public void setUserId(String id)    {
            mUserId = id;
        }

        public int getLoadType() {
            return mLoadType;
        }

        public void setLoadType(int loadType) {
            mLoadType = loadType;
        }

        public void startTaskTripDetails(TripDetailsActivity activity) {
            if (!mIsRunningTripDetailsTask) {
                mIsRunningTripDetailsTask = true;
                mTaskTripDetails = new TripDetailsTask(activity, mTripId);
                mTaskTripDetails.execute();
            }
        }

        public void startTaskFocused(TripDetailsActivity activity, boolean isFocused) {
            if (!mIsRunningFocusedTask) {
                mIsRunningFocusedTask = true;
                mTaskFocused = new FocusedTask(activity, mTrip.getId(), isFocused);
                mTaskFocused.execute();
            }
        }

        public boolean getIsRunningTripDetailsTask() {
            return mIsRunningTripDetailsTask;
        }

        public void setIsRunningTripDetailsTask(boolean isRunning) {
            mIsRunningTripDetailsTask = isRunning;
        }

        public boolean getIsRunningFocusedTask() {
            return mIsRunningFocusedTask;
        }

        public void setIsRunningFocusedTask(boolean isRunning) {
            mIsRunningFocusedTask = isRunning;
        }

        public boolean getRanOnce() {
            return mRanOnce;
        }

        public void setRanOnce(boolean ranOnce) {
            mRanOnce = ranOnce;
        }

        public void cancelTasks() {
            if (mTaskTripDetails != null) {
                mTaskTripDetails.setActivity(null );
                mTaskTripDetails.cancel(true);
            }
        }

        public boolean getIsTaskRunning() {
            return mIsRunningTripDetailsTask;
        }
    }

    private class RemoteResourceManagerObserver1 implements Observer {
        @Override
        public void update(Observable observable, Object data) {
            mHandler1.post(mRunnableUpdateTripPhoto);
        }
    }

    private class RemoteResourceManagerObserver2 implements Observer {
        @Override
        public void update(Observable observable, Object data) {
            mHandler2.post(mRunnableUpdateUserPhoto);
        }
    }

    private Runnable mRunnableUpdateTripPhoto = new Runnable() {
        @Override
        public void run() {
            ensureTripPhoto(mStateHolder.getTrip().getPhoto());
        }
    };

    private Runnable mRunnableUpdateUserPhoto = new Runnable() {
        @Override
        public void run() {
            ensureUserPhoto(mStateHolder.getTrip());
        }
    };
}
